package cn.com.stary1993.kite.captcha.demo.controller;

import cloud.tianai.captcha.common.response.ApiResponse;
import cn.hutool.core.util.StrUtil;
import io.github.stary1993.kite.captcha.support.behavior.BehaviorImageCaptcha;
import io.github.stary1993.kite.captcha.support.behavior.BehaviorImageCaptchaTrack;
import io.github.stary1993.kite.captcha.support.simple.SimpleImageCaptcha;
import io.github.stary1993.kite.captcha.support.simple.SimpleImageCaptchaType;
import io.github.stary1993.kite.captcha.vo.BehaviorCaptchaVO;
import io.github.stary1993.kite.captcha.vo.CaptchaResponse;
import io.github.stary1993.kite.captcha.vo.SimpleCaptchaVO;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@CrossOrigin
@RequiredArgsConstructor
@RestController
@RequestMapping("/captcha")
public class CaptchaDemoController {

    private final SimpleImageCaptcha simpleImageCaptcha;

    private final BehaviorImageCaptcha behaviorImageCaptcha;



    @SneakyThrows
    @GetMapping("/simple/gen")
    public CaptchaResponse<SimpleCaptchaVO> genSimpleImageCaptcha(HttpServletResponse response, @RequestParam("type") String type, String format, String timestamp) {
        if (StrUtil.isNotBlank(format) && StrUtil.equals(format, "json")) {
            return simpleImageCaptcha.generate(SimpleImageCaptchaType.valueOf(type), timestamp);
        } else {
            simpleImageCaptcha.out(SimpleImageCaptchaType.valueOf(type), timestamp, response.getOutputStream());
        }
        return null;
    }


    @PostMapping("/simple/check")
    public ApiResponse checkSimpleImageCaptcha(@RequestParam(value = "id", required = false) String id,
                                              @RequestParam("type") String type,
                                              @RequestParam(value = "timestamp", required = false) String timestamp,
                                              @RequestParam("code") String code) {
        if (id != null) {
            return simpleImageCaptcha.matching(id, SimpleImageCaptchaType.valueOf(type), timestamp, code);
        } else {
            return simpleImageCaptcha.matching(SimpleImageCaptchaType.valueOf(type), timestamp, code);
        }
    }



    @GetMapping("/behavior/gen")
    public CaptchaResponse<BehaviorCaptchaVO> genBehaviorImageCaptcha(@RequestParam("type") String type, @RequestParam(value = "obfuscate", required = false) boolean obfuscate, @RequestParam(value = "tag", required = false) String tag) {
        return behaviorImageCaptcha.generate(type, obfuscate, tag);
    }


    @PostMapping("/behavior/check")
    public ApiResponse checkBehaviorImageCaptcha(@Valid @RequestBody MatchingData matchingData) {
        return behaviorImageCaptcha.matching(matchingData.getId(), matchingData.getData());
    }

    @Data
    static class MatchingData {
        @NotBlank(message = "id不能为空")
        private String id;
        @NotNull(message = "data不能为空")
        private BehaviorImageCaptchaTrack data;
    }
}
